package com.gxg.demo10.mydemo11.design;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.ColorUtils;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gxg.demo10.mydemo11.BaseRvAdapter;
import com.gxg.demo10.mydemo11.R;
import com.socks.library.KLog;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：Administrator on 2018/3/12 09:22
 * 邮箱：android_gaoxuge@163.com
 */
public class DesignTestActivity extends AppCompatActivity {
    @Bind(R.id.image_collapsing)
    ImageView imageCollapsing;
    @Bind(R.id.toolbar_collapsing)
    Toolbar toolbarCollapsing;
    @Bind(R.id.collapsing_toolbar)
    CollapsingToolbarLayout collapsingToolbar;
    @Bind(R.id.appbar)
    AppBarLayout appbar;
    @Bind(R.id.recyclerview)
    RecyclerView recyclerview;
    @Bind(R.id.fab_detail)
    FloatingActionButton fabDetail;
    @Bind(R.id.main_content)
    CoordinatorLayout mainContent;
    @Bind(R.id.v_statusbar)
    View vStatusBar;
    @Bind(R.id.tv_title)
    TextView tv_title;
    private int toobarHeight;
    private List<String> titles = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_design_test);
        ButterKnife.bind(this);


        initData();


        appbar.post(new Runnable() {


            @Override
            public void run() {
//                KLog.e("sss  "+appbar.getTotalScrollRange());
                transEndY = appbar.getTotalScrollRange();
                 toobarHeight = tv_title.getHeight();
                KLog.e("sss   "+ toobarHeight);
            }
        });

        appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if(verticalOffset==0){
                    toolbarCollapsing.setBackgroundColor(ColorUtils.setAlphaComponent(ContextCompat.getColor(DesignTestActivity.this,R.color.orange_dft), 0));
                }else{
                    int statusBarAlpha = getTransAlpha(Math.abs(verticalOffset));
                    toolbarCollapsing.setBackgroundColor(ColorUtils.setAlphaComponent(ContextCompat.getColor(DesignTestActivity.this,R.color.orange_dft), (statusBarAlpha)));
                }
                tv_title.setVisibility(Math.abs(verticalOffset) > toobarHeight ? View.VISIBLE : View.GONE);
            }
        });

        if(Build.VERSION.SDK_INT==Build.VERSION_CODES.KITKAT){
            setStatusBarHeight(getStatusBarHeight());
        }



    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 设置状态栏高度
     *
     * @param statusBarHeight
     */
    public void setStatusBarHeight(int statusBarHeight) {
        ViewGroup.LayoutParams params = vStatusBar.getLayoutParams();
        params.height = statusBarHeight;
        vStatusBar.setLayoutParams(params);
    }
    /**
     * 获取状态栏高度
     *
     * @return
     */
    public int getStatusBarHeight() {
        //获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            return getResources().getDimensionPixelSize(resourceId);
        }
        return 0;
    }

    private void initData() {
        titles.clear();
        for (int i = 0; i < 20; i++) {
            titles.add("商品"+i);
        }
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        BaseRvAdapter adapter = new BaseRvAdapter(new ArrayList<String>());
        recyclerview.setAdapter(adapter);

        adapter.setNewData(titles);
    }

    //渐变开始位置
    private int transStartY = 50;
    //渐变结束位置
    private int transEndY = 300;
    /**
     * 获取透明度
     *
     * @return
     */
    private int getTransAlpha(int scrollY) {
        if (transStartY != 0) {
            if (scrollY <= transStartY) {
                return 0;
            } else if (scrollY >= transEndY) {
                return 225;
            } else {
                return ((int) ((scrollY - transStartY) * 255 / (transEndY - transStartY) ))<=225?((int) ((scrollY - transStartY) * 255 / (transEndY - transStartY) )):225;
            }
        } else {
            if (scrollY >= transEndY) {
                return 255;
            }
            return (int) ((transEndY - scrollY) / transEndY * 255);
        }
    }

}
